﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using S4Store.Persistent;
using S4Store.Domain;
using LX.Core.Query;
using S4Store.Service.Valid.StoreValid;
using S4Store.Domain.SellerCarBrand;

namespace S4Store.Service
{
    public class SellerCarBrandService : BaseService
    {

        public IQueryable<Row> GetQuery()
        {
            return this.GetQuery<Row>();
        }

        public Table GetTable(int storeid)
        {
            return GetTable(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, storeid);
        }

        public Table GetTable(int year,int month,int day,int storeid,bool getlastmonth=true)
        {

            Table table = new Table();
            table.Month = new DateTime(year, month, day);
            table.CurrentDate = new DateTime(year, month, day).MonthLastOrNow();
            table.Store = this.GetQuery <Store>().FirstOrDefault(t => t.Id == storeid);
            var sellers = this.GetQuery<Employee>().Where(t => t.StoreId == storeid).ToList();
            var carbrands =this.GetQuery<CarBrand>().ToList();
            var rows = this.GetQuery().Where(t => t.Seller.StoreId == storeid&&t.Month.Year==year&&t.Month.Month==month).ToList();
            foreach (var seller in sellers)
            {
                foreach (var brand in carbrands)
                {
                    var row = rows.FirstOrDefault(t => t.SellerId == seller.Id && t.CarBrandId == brand.Id);
                    if (row == null)
                    {
                        row = new Row
                        {
                            CarBrandId = brand.Id,
                            CarBrand = brand,
                            SellerId = seller.Id,
                            Seller = seller
                        };

                    }
                    row.Table = table;
                    table.Rows.Add(row);
                }
            }
            DateTime last=table.Month.AddMonths(-1);
            if (getlastmonth)
            {
                table.LastMonthData = GetTable(last.Year, last.Month, last.LastDay(), storeid, false);
            }
            return table;
        }

        public void Modify(List<Row> rows)
        {
            if (rows == null||rows.Count()==0) return;
            foreach (var row in rows)
            {
                if (row.Id != 0)
                {
                    var dbrow = this.GetQuery().FirstOrDefault(t => t.Id == row.Id);
                    if (dbrow != null)
                    {
                        this.GetSet<Row>().Remove(dbrow);
                    }
                    row.Id = 0;
                }
                this.GetSet<Row>().Add(row);
            }
            //int id=rows[0].SellerId;
            //var seller=this.GetQuery<Seller>().FirstOrDefault(t=>t.Id==id);
           // var table = this.GetQuery<Table>().FirstOrDefault(t => t.StoreId == seller.StoreId);
            
        }
    }
}
